<template>
    <div v-if="visible" class="custom-modal">
      <div class="modal-overlay" @click="handleCancel"></div>
      <div class="modal-content">
        <div class="modal-header">
          <h3>资产详情</h3>
          <button class="close-btn" @click="handleCancel" aria-label="关闭">×</button>
        </div>
        <div class="modal-body" v-if="assetDetail.type == '1'">
          <!-- 基础信息 section -->
          <div class="detail-section">
            <h4 class="with-left-bar">基础信息</h4>
            <div class="detail-grid">
              <div 
                class="detail-item" 
                v-for="(item, index) in basicInfoFields" 
                :key="index"
                :v-if="item.visible"
              >
                <span class="label">{{ item.label }}：</span>
                <span class="value">{{ assetDetail[item.prop] || item.fallback || '-' }}</span>
              </div>
            </div>
          </div>
  
          <!-- 空间资产信息 section -->
          <div class="detail-section" v-if="assetDetail.type === '1'">
            <h4 class="with-left-bar">空间资产信息</h4>
            <div class="detail-grid">
              <div 
                class="detail-item" 
                v-for="(item, index) in spaceInfoFields" 
                :key="index"
                :v-if="item.visible"
              >
                <span class="label">{{ item.label }}：</span>
                <span class="value">
                  {{ assetDetail[item.prop] || item.fallback || '-' }}
                  <template v-if="item.unit">{{ item.unit }}</template>
                </span>
              </div>
            </div>
          </div>
  
          <!-- 设备资产信息 section -->
          <div class="detail-section" v-if="assetDetail.type === '2'">
            <h4 class="with-left-bar">设备资产信息</h4>
            <div class="detail-grid">
              <div 
                class="detail-item" 
                v-for="(item, index) in equipmentInfoFields" 
                :key="index"
                :v-if="item.visible"
              >
                <span class="label">{{ item.label }}：</span>
                <span class="value">{{ assetDetail[item.prop] || item.fallback || '-' }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="cancel-btn" @click="handleCancel">关闭</button>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      assetDetail: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        // 字段配置化，方便动态增减和维护
        basicInfoFields: [
          { label: '资产编码', prop: 'assetCode', visible: true },
          { label: '资产名称', prop: 'assetName', visible: true },
          { label: '资产类型', prop: 'assetType_dictText', visible: true },
          { label: '资产状态', prop: 'status_dictText', visible: true },
          { label: '负责人', prop: 'chargePerson', visible: true },
          { label: '联系电话', prop: 'chargeMobile', visible: true }
          // 可在此处动态添加或移除字段
        ],
        spaceInfoFields: [
          { label: '空间类型', prop: 'spaceType_dictText', visible: true },
          { label: '空间子类', prop: 'spaceSubtype_dictText', visible: true },
          { label: '详细地址', prop: 'place', visible: true },
          { label: '建筑面积', prop: 'buildingArea', visible: true, unit: '㎡' },
          { label: '可租面积', prop: 'rentableArea', visible: true, unit: '㎡' },
          { label: '楼层信息', prop: 'floorInfo', visible: true }
          // 可在此处动态添加或移除字段
        ],
        equipmentInfoFields: [
          { label: '设备分类', prop: 'equipmentCategory_dictText', visible: true },
          { label: '存放位置', prop: 'place', visible: true },
          { label: '品牌', prop: 'brand', visible: true },
          { label: '规格型号', prop: 'model', visible: true },
          { label: '制造商', prop: 'manufacturer', visible: true },
          { label: '出厂日期', prop: 'factoryDate', visible: true }
          // 可在此处动态添加或移除字段
        ]
      };
    },
    methods: {
      handleCancel() {
        this.$emit('close');
      }
    }
  };
  </script>
  
  <style scoped>
    .with-left-bar {
        position: relative;
        padding-left: 12px; /* 给竖杠留出空间 */
    }

    .with-left-bar::before {
        content: '';
        position: absolute;
        left: 0;
        top: 2px;
        bottom: 10px;
        width: 3px;
        background-color: #2a4f7a; /* 使用现有主题色中的强调色 */
        border-radius: 2px; /* 轻微圆角，更美观 */
    }

  .custom-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
  }
  
  .modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(2px);
    transition: opacity 0.3s ease;
  }
  
  .modal-content {
    position: relative;
    background: #0a1f44;
    border: 1px solid #1e3a5f;
    border-radius: 8px;
    width: 100%;
    max-width: 1200px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.7);
  }
  
  .modal-header {
    background: #102a5c;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #1e3a5f;
  }
  
  .modal-header h3 {
    color: #d4e1f5;
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  }
  
  .close-btn {
    background: none;
    border: none;
    color: #8ba6d9;
    font-size: 24px;
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
  }
  
  .close-btn:hover {
    color: #fff;
    background-color: rgba(255, 255, 255, 0.1);
  }
  
  .modal-body {
    padding: 20px;
    overflow-y: auto;
    color: #d4e1f5;
    line-height: 1.6;
  }
  
  /* 自定义滚动条 */
  .modal-body::-webkit-scrollbar {
    width: 6px;
  }
  
  .modal-body::-webkit-scrollbar-track {
    background: #102a5c;
    border-radius: 3px;
  }
  
  .modal-body::-webkit-scrollbar-thumb {
    background: #1e3a5f;
    border-radius: 3px;
  }
  
  .modal-body::-webkit-scrollbar-thumb:hover {
    background: #2a4f7a;
  }
  
  .detail-section {
    margin-bottom: 24px;
    padding-bottom: 12px;
    /* border-bottom: 1px solid #1e3a5f; */
  }
  
  .detail-section:last-child {
    margin-bottom: 0;
    border-bottom: none;
  }
  
  .detail-section h4 {
    margin: 0 0 16px 0;
    color: #a9c4e8;
    font-size: 16px;
    font-weight: 600;
    padding-bottom: 8px;
    border-bottom: 1px solid #1e3a5f;
  }
  
  /* 核心：动态网格布局 - 始终保持两列 */
  .detail-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr); /* 固定两列 */
    gap: 16px; /* 列间距 */
    row-gap: 12px; /* 行间距 */
  }
  
  /* 当字段数量为奇数时，最后一项自动占满两列 */
  .detail-grid > .detail-item:last-child:nth-child(odd) {
    grid-column: span 2;
  }
  
  .detail-item {
    display: flex;
    flex-wrap: wrap;
    padding: 4px 0;
  }
  
  .label {
    font-weight: 600;
    color: #a9c4e8;
    min-width: 100px;
    margin-right: 8px;
  }
  
  .value {
    color: #d4e1f5;
    flex: 1;
    word-break: break-word;
  }
  
  .modal-footer {
    background: #102a5c;
    padding: 14px 20px;
    border-top: 1px solid #1e3a5f;
    text-align: right;
  }
  
  .cancel-btn {
    background: #1e3a5f;
    color: #d4e1f5;
    border: 1px solid #2a4f7a;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
  }
  
  .cancel-btn:hover {
    background: #2a4f7a;
    transform: translateY(-1px);
  }
  
  .cancel-btn:active {
    transform: translateY(0);
  }
  
  /* 响应式调整 - 小屏幕自动变为单列 */
  @media (max-width: 768px) {
    .detail-grid {
      grid-template-columns: 1fr;
    }
    
    .detail-grid > .detail-item:last-child:nth-child(odd) {
      grid-column: span 1;
    }
    
    .modal-content {
      max-height: 90vh;
    }
  }
  </style>
  